import json
from urllib import parse

import excelUtil

searchUserPath = '/rest/nebula/search/user?'

def ksSearch(flow):
    # https://apissl.gifshow.com/rest/nebula/search/user?mod=HUAWEI%28TAS-AL00%29&lon=116.41025&country_code=cn&kpf=ANDROID_PHONE&did=ANDROID_2daa478a8266f80a&kpn=NEBULA&net=WIFI&app=0&oc=HUAWEI&egid=DFPF7ED44021AB74454EA3A8BD82E481AFB76FD769BDB109454A61FB97196520&apptype=22&ud=1528314752&hotfix_ver=&c=HUAWEI&sys=ANDROID_5.1.1&appver=1.5.3.86&ftt=&language=zh-cn&iuid=&lat=39.916411&did_gt=1588660756449&ver=1.5&max_memory=192
    userHttpStart = 'https://apissl.ksapisrv.com/rest/nebula/search/user?'
    apis2Start = 'https://apis2.gifshow.com/rest/nebula/search/user?'
                # https://apissl.gifshow.com/rest/nebula/search/user?
    if flow.request.url.find(searchUserPath) >= 0:
        text = json.loads(flow.response.text)
        keyword = parse.unquote(flow.request.text.split('&')[0].replace('keyword=', ''))
        print('==========keyword==========================' + keyword)
        resultData = [[keyword]]
        if text != 'null':
            users = text.get('users')
            if (users != 'null' and len(users) > 0):
                firstUser = users[0]
                print(firstUser)
                # 看了这么多快手，还是「放飞梦想真皮女鞋」最好玩了！ https://v.kuaishouapp.com/s/yaIDxmtw 复制此链接，打开【快手】直接观看！
                # https://live.kuaishou.com/profile/
                userName = firstUser.get('user_name')
                shareUrl = 'https://live.kuaishou.com/profile/' + firstUser.get('kwaiId')
                resultData[0] = [keyword, userName, shareUrl]
        excelUtil.write_excel_xls_append(excelUtil.FILE_PATH, '快手', resultData)